from core.plane import Plane

import simpy
class Broker(object):
    plane_cls = Plane

    def __init__(self, env:simpy.Environment ,event:simpy.Event, plane_configs:list):
        self.env = env
        self.event = event
        self.simulation = None
        self.board = None
        self.destroyed = False
        self.plane_configs = plane_configs
        # self.df = df # dataframe



    def attach(self, simulation):
        self.simulation = simulation
        self.board = simulation.board

    def run(self):

        for plane_config in self.plane_configs:
            # 动态设置任务的到达时间
            # assert plane_config.submit_time >= self.env.now
            # 静态保障
            yield self.env.timeout(0)
            plane = Broker.plane_cls(self.env, plane_config)
            print('a plane arrived at time %f' % self.env.now)
            self.board.add_plane(plane)
        self.event[0].succeed()
        self.event[0] = self.env.event()
        # 生成新的调度事件，防止重复调用

        self.destroyed = True
